home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: beginner question
- Date: 3 Apr 1996 17:59:19 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4jue9n$gm1@sparcserver.lrz-muenchen.de>
- References: <4jc3sr$1ggu@uvaix3e1.comp.UVic.CA> <4jdo7l$de2@sparcserver.lrz-muenchen.de> <315AFED2.7466@willows.com> <315F525A.4A1CD496@alcyone.com> <31625C30.12AA@sooner.net>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- Eddie Bush <edwbush@sooner.net> writes:
-
- >Erik Max Francis wrote:
- >>
- >> Tarang Deshpande wrote:
- >>
- >> > So then what does the following mean:
- >> >
- >> > struct _FOO
- >> > {
- >> > int bar;
- >> > } FOO;
- >> >
- >> > struct _FOO s1;
- >> > FOO s2;
- >> >
- >> > Why?
- >>
- >> This is a compiler error.
-
- >I'm sorry. I don't see an error -- just two different variables of the same type that happen to
- >be declared using two different (and correct) methods.
-
- The first is, as Erik states correctly, a valid definition for s1, whereas
- the second is similar to
-
- int a;
- a b;
-
- Without an operator between "a" and "b", this is not valid in C. A compiler
- must issue a diagnostic message for a program that contains that statement.
-
- >>
- >> struct _FOO { int bar; } FOO;
- >>
- >> declares a structure called struct _FOO and an instance of that structure FOO.
- >> The further declaration
-
- >The initial declaration of the structure doesn't make available FOO for
- >manipulation. FOO is a
- >type. ...isn't it?
-
- No, it's not! "FOO" is a variable, and "struct _FOO" is it's type.
-
- >I always understood that 'struct _FOO' is the 'tag name' (the name of the
- >structure', and that FOO would be a type which you have defined (by the typedef struct _FOO) to
- >be the structure _FOO.
-
- There _is_ no "typedef" in "struct _FOO { int bar; } FOO;", nor was there
- a "typedef" in the original article. Erik's diagnosis seems to be correct.
- The original poster "missed" the "typedef".
-
- >Also, from what I understand, you would do it like this:
-
- >typedef struct {
- > int num;
- >} FOOBAR;
-
- Type "FOOBAR" is an anonymous struct.
-
- >This would be if you intended to use the type statically. Or, you could:
-
- >typedef struct FOO {
- > int num;
- >} *BAR;
-
- Type "BAR" is a pointer to "struct FOO".
-
- >if you happened to want to allocate the variable dynamically.
-
- The main question is whether you want to declare a pointer type or
- not. "BAR" can well be used without any dynamic memory allocation.
-
- struct FOO x;
- BAR y = &x;
-
- >Notice, that I left the 'tag name' off of the variable which I intend to
- >use statically. That is
- >because it is not needed -- you only need it to allocate memory dynamically:
-
- >BAR new_bar;
-
- >new_bar = (BAR) malloc (sizeof (struct FOO));
-
- Have you tried
-
- new_bar = malloc(sizeof(*new_bar));
-
- The "struct tag" is needed for, e.g., recursive data types, but _not_
- for memory allocation.
-
- >Maybe this helps (and doesn't confuse) someone. I felt very confused by
- >the string of preceding
- >messages -- maybe it's the time of day. Who knows...
-
- Get some sleep. Read the "string of preceding messages" again. Esp. the
- message from Erik.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
-